package com.wiscamp.ninechapters.threeparties.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import cube.ddd.aspect.HandleServiceException;
import cube.ddd.domain.BaseDomainService;
import jakarta.validation.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * SMS Service
 */
@Service
public class SmsService extends BaseDomainService {
    private static final String signName = "";
    private static final String accessKeyId = "LTAIbm76PAa6Cciv";
    private static final String accessSecret = "ndylF7gxnORJFpRpkbLb5fZmyhEc8M";
    public final String templateCode = "SMS_168341570";

    /**
     * Sends a sms
     *
     * @param phoneNumber
     * @param templateCode
     * @param param
     * @return
     */
    @HandleServiceException
    public boolean sendSms(@NotBlank String phoneNumber, @NotBlank String templateCode, @NotBlank String param) {
        var profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        var client = new DefaultAcsClient(profile);
        var request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", param);
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException cex) {
            logger.error("Failed to send sms message.", cex);
        }
        String data = response.getData();
        return data.contains("OK");
    }
}
